package com.wolfalpha.utron.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.ItemIdea;

import java.util.List;

/**
 * (创意）列表适配器
 * <p/>
 * Created by luodon on 15/8/27.
 */
public class ItemIdeaAdapter extends BaseAdapter {


    private LayoutInflater inflater;
    private Context context;

    private List<ItemIdea> ideas = null;

    public ItemIdeaAdapter(Context context, List<ItemIdea> rs) {
        inflater = LayoutInflater.from(context);
        if (rs != null) {
            ideas = rs;
        }
        this.context = context;

    }

    /**
     * 改变数据
     *
     * @param rs
     */
    public void setData(List<ItemIdea> rs) {
        if (rs != null) {
            ideas = rs;
        }

        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return ideas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_idea, null);
            viewHolder = new ViewHolder();
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.item_tv_idea_title);
            viewHolder.tvViews = (TextView) convertView.findViewById(R.id.item_tv_idea_views);
            viewHolder.tvContent = (TextView) convertView.findViewById(R.id.item_tv_idea_content);
            viewHolder.tvFrom = (TextView) convertView.findViewById(R.id.item_tv_idea_from);
            viewHolder.tvDir = (TextView) convertView.findViewById(R.id.item_tv_idea_dir);
            viewHolder.ivHead = (ImageView) convertView.findViewById(R.id.item_iv_idea_head);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (ideas != null) {
//        //设置控件内容
            viewHolder.tvTitle.setText(ideas.get(position).getTitle());
            viewHolder.tvViews.setText(String.valueOf(ideas.get(position).getViews()));
            viewHolder.tvContent.setText(ideas.get(position).getContent());
            viewHolder.tvFrom.setText("来自: " + ideas.get(position).getFrom());
            String dir = AppContext.getInstance().getDirs().get(ideas.get(position).getDir() - 1001);
            if (dir == null) viewHolder.tvDir.setVisibility(View.GONE);
            else {
                viewHolder.tvDir.setText(dir);
                if (dir.length() > 2) {
                    viewHolder.tvDir.setBackgroundResource(R.drawable.corner_text_orange_normal);
                } else {
                    viewHolder.tvDir.setBackgroundResource(R.drawable.corner_text_green_normal);
                }
            }
//        //设置图片
            Picasso.with(context)
                    .load(ideas.get(position).getUser_head())
                    .placeholder(R.mipmap.head_default)
                    .into(viewHolder.ivHead);
        }
        return convertView;
    }


    /**
     * 控件
     */
    class ViewHolder {
        private TextView tvTitle;       //标题
        private TextView tvViews;       //浏览次数
        private TextView tvContent;     //内容
        private TextView tvFrom;        //来自
        private TextView tvDir;         //方向
        private ImageView ivHead;       //头像
    }
}
